#include "InComFileOperation.h"

#define DEF_TAG             "FileOperation"

Com_s32 Ext_InComFile_Access(Com_ps8 file_name)
{
    return (access(file_name, F_OK) == 0) ? 0 : -1;
}

Com_s32 Ext_InComFile_Creat(Com_ps8 file_name)
{
    Com_s32 fd;

    fd = creat(file_name,0777);
    if(fd < 0)
    {
        PRT_ERR(DEF_TAG,"creat %s failed\n",file_name);
        return fd;
    }

    close(fd);
    return 0;
}

Com_void Ext_InComFile_JudgeCreat(Com_ps8 file_name)
{
    if(Ext_InComFile_Access(file_name) == DEF_COM_FILE_NOT_EXIST)
    {
        Ext_InComFile_Creat(file_name);
    }
}

Com_s32 Ext_InComFile_Remove(Com_ps8 file_name)
{
    return remove(file_name);
}

Com_void Ext_InComFile_JudgeRemove(Com_ps8 file_name)
{
    if(Ext_InComFile_Access(file_name) == DEF_COM_FILE_EXIST)
    {
        Ext_InComFile_Remove(file_name);
    }
}

Com_s32 Ext_InComFile_ChooseMode(Com_ps8 file_name,Com_s32 mode)
{
    switch(mode)
    {
    case E_REMOVE_FILE :
        Ext_InComFile_Remove(file_name);
        return 0;

    case E_TOUCH_FILE :
        Ext_InComFile_Creat(file_name);
        return 0;

    case E_CHECK_FILE :
    {
        if(Ext_InComFile_Access(file_name) == 0)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

    default:
        return -1;
    }

    return -1;
}

Com_s32 Ext_InComFile_GetSize(Com_ps8 filename)
{
    Com_s32 size;
    struct stat statbuf;

    stat(filename,&statbuf);
    size = statbuf.st_size;

    return size;
}

Com_s64 Ext_InComFile_GetLen(const char *filePath)
{
    FILE *file;
    long int size;

    /** 打开文件 */
    file = fopen(filePath, "r");
    if (file == NULL) {
        perror("Can't open file ");
        PRT_ERR(DEF_TAG,"%s !\n", filePath);
        return -1;
    }

    /** 将文件指针移动到文件末尾 */
    fseek(file, 0, SEEK_END);

    /** 获取文件大小 */
    size = ftell(file);

    /** 将文件指针移动回文件开头 */
    fseek(file, 0, SEEK_SET);

    /** 关闭文件 */
    fclose(file);

    return size;
}

Com_s32 Ext_InComFile_Write(Com_ps8 file_name ,Com_ps8 buffer,Com_s32 size)
{
    Com_s32 fd = open(file_name, O_CREAT|O_WRONLY|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
    if(fd > 0)
    {
        if(size == 0)
        {
            write(fd, buffer, strlen(buffer));
        }
        else
        {
            write(fd, buffer, size);
        }

        close(fd);
    }

    return 0;
}

Com_s32 Ext_InComFile_ReadStr(Com_ps8 file_name ,Com_ps8 buffer,Com_s32 size)
{
    Com_s32 ret = 0;
    Com_s32 fd = -1;

    fd = open(file_name, O_RDONLY,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
    if(fd > 0)
    {
        ret = read(fd, buffer, size);
        close(fd);
    }
    return ret;
}

Com_s32 Ext_InComFile_ReadInt(char *file_name,_OUT int *val)
{
    int fd = -1;
    int ret = -1;
    char buffer[36];

    memset(buffer,0,sizeof(buffer));

    fd = open(file_name, O_RDONLY);
    if(fd > 0)
    {
        ret = read(fd, buffer, sizeof(buffer));
        close(fd);

        if (ret > 0)
        {
            *val = atoi(buffer);
        }
    }
    return ret;
}

Com_s32 Ext_InComFile_BasicIOAppend(Com_ps8 file_name ,Com_ps8 buffer,Com_s32 size)
{
    FILE *fp;

    fp = fopen(file_name,"ab+");
    if(fp == NULL)
    {
        printf("fail to open file\n");
        return -1;
    }

    fwrite(buffer,1,size,fp);
    fclose(fp);

    return 0;
}

Com_s32 Ext_InComFile_BasicIORead(Com_ps8 file_name ,Com_ps8 buffer,Com_s32 size)
{
    FILE *fp;
    int ret = 0;

    fp = fopen(file_name,"rb");
    if(fp == NULL)
    {
        printf("fail to open file\n");
        return ret;
    }
    ret = fread(buffer,1,size,fp);
    fclose(fp);

    return ret;
}

Com_s32 Ext_InComFile_BasicIODelFront(Com_ps8 file_name ,Com_s32 size)
{
    FILE *pfile;
    FILE *temp_file= NULL;
    Com_s8 temp_file_name[48] = {0};
    Com_s8 buffer[128] = {0};
    int ret = 0;

    if(Ext_InComFile_GetSize(file_name) < size)
    {
        size = Ext_InComFile_GetSize(file_name);
        PRT_WARN(DEF_TAG,"file smaller then del size\n");
    }

    pfile = fopen(file_name,"r");
    if(pfile == NULL)
    {
        PRT_ERR(DEF_TAG,"fail to open file\n");
        return -1;
    }

    sprintf(temp_file_name,"%s%s",file_name,"_old");
    temp_file = fopen(temp_file_name, "w+");
    if(temp_file == NULL)
    {
        fclose(pfile);
        return -2;
    }

    fseek(pfile, 1L * size, SEEK_SET);
    while( (ret = fread(buffer, sizeof(char), sizeof(buffer), pfile)) > 0)
    {
        int iRet = fwrite( buffer, sizeof( char ),  ret, temp_file);
        if(iRet < ret)
        {
            PRT_ERR(DEF_TAG,"write temp file error!!!\n");
            fclose(pfile);
            fclose(temp_file);
            remove(temp_file_name);
            return -3;
        }

        memset(buffer, 0, sizeof(buffer));
    }

    fclose(pfile);
    fclose(temp_file);
    rename(temp_file_name, file_name);

    return 0;
}

Com_s32 Ext_InComFile_ReadDir(Com_ps8 path,_OUT Com_ps8 buf)
{
    DIR *dp;
    struct dirent *entry;

    dp = opendir(path);
    if (dp == NULL)
    {
        perror("opendir");
        return -1;
    }

    while ((entry = readdir(dp)))
    {
        /** 跳过'.'和'..'目录 **/
        if (entry->d_name[0] != '.')
        {
            //PRT_INFO(DEF_TAG,"%s\n", entry->d_name);
            Com_s8 file_name[260];

            memset(file_name,0,sizeof(file_name));
            sprintf(file_name,"%s;",entry->d_name);
            strcat(buf,file_name);
        }
    }

    closedir(dp);
    return 0;
}

Com_s32 Ext_InComFile_Copy(char *dest, char *src)
{
    // 以二进制读取模式打开源文件
    FILE *sourceFile = fopen(src, "rb");
    if (sourceFile == NULL)
    {
        perror("Error opening source file");
        return -1;
    }

    // 以二进制写入模式打开目标文件
    FILE *destFile = fopen(dest, "wb");
    if (destFile == NULL)
    {
        // 关闭已打开的源文件
        fclose(sourceFile);
        perror("Error opening destination file");
        return -1;
    }

    // 缓冲区大小可以根据需要调整
    const size_t buffer_size = Ext_InComFile_GetSize(src) + 1;
    char buffer[buffer_size];
    size_t bytesRead;

    memset(buffer,0,sizeof(buffer));
    // 循环读取并写入文件
    while ((bytesRead = fread(buffer, 1, buffer_size, sourceFile)) > 0)
    {
        fwrite(buffer, 1, bytesRead, destFile);
    }

    // 关闭文件
    fclose(sourceFile);
    fclose(destFile);

    // 成功返回0
    return 0;
}
